package com.yanqu.road.server.manger.activity.pixiu;

import com.yanqu.road.dao.impl.activity.pixiu.PiXiuConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.pixiu.PiXiuUserData;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.PiXiuProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.pixiu.PiXiuModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.property.Property;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class PiXiuActivityMgr extends TempMgr {

    static PiXiuConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload PiXiu Activity start");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PiXiu.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no PiXiu Activity in show time");
            config = null;

        } else {

            ActivityInfo activityInfo = openActivityInfoList.get(0);
            PiXiuConfig piXiuConfig = new PiXiuConfig(activityInfo);
            int tmpActivityId = activityInfo.getActivityId();
            PiXiuConfigDaoImpl dao = new PiXiuConfigDaoImpl();
            piXiuConfig.setAmuseInfoList(dao.getPiXiuAmuseInfoList(tmpActivityId));
            //初始化配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
            if(activityConfigMap != null){
                if (activityConfigMap.containsKey(eActivityConfigType.PIXIU_ITEMID.getName())) {
                    piXiuConfig.setItemId(activityConfigMap.get(eActivityConfigType.PIXIU_ITEMID.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.PIXIU_CLAW_RATE.getName())) {
                    piXiuConfig.setClawWeight(activityConfigMap.get(eActivityConfigType.PIXIU_CLAW_RATE.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.PIXIU_HEAD_RATE.getName())) {
                    piXiuConfig.setHeadWeight(activityConfigMap.get(eActivityConfigType.PIXIU_HEAD_RATE.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.PIXIU_BUTT_RATE.getName())) {
                    piXiuConfig.setButtWeight(activityConfigMap.get(eActivityConfigType.PIXIU_BUTT_RATE.getName()).getIntValue());
                }
                piXiuConfig.initWeightList();
            }
            config = piXiuConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(PiXiuModule.class).activityReload();
        }

        getLogger().info("reload PiXiu Activity finish");
    }

    public static PiXiuConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo() {
        if(config == null) {
            return null;
        }
        return config.getActivityInfo();
    }

    public static Property getProperty(PiXiuUserData userData, PiXiuConfig config) {

        Property all = new Property();

        all.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(userData.getFootBase()));

        all.addProperty(userData.getHeadReward(), BigInteger.valueOf(userData.getHeadBase()));

        all.addProperty(userData.getButtReward(), BigInteger.valueOf(userData.getButtBase()));

        return all;
    }

    public static PiXiuProto.PiXiuUserDataTemp.Builder builder(PiXiuUserData userData){
        PiXiuProto.PiXiuUserDataTemp.Builder builder = PiXiuProto.PiXiuUserDataTemp.newBuilder();
        builder.setHappyValue(userData.getHappyValue());
        builder.setHeadReward(userData.getHeadReward());
        builder.setButtReward(userData.getButtReward());
        builder.setConditionValue(userData.getConditionValue());
        builder.setUseTokenNum(userData.getUseTokenNum());
        builder.setHeadBase(userData.getHeadBase());
        builder.setButtBase(userData.getButtBase());
        builder.setFootBase(userData.getFootBase());
        builder.setGetReward(userData.getGetReward());
        return builder;
    }


}
